package com.example.onlineeducation.repository

import com.example.onlineeducation.data.entity.ExamEntity
import com.example.onlineeducation.data.entity.TopicEntity
import com.example.onlineeducation.mapper.ExamMapper
import com.example.onlineeducation.mapper.TopicMapper
import com.example.onlineeducation.net.ApiService
import com.yc.jetpacklib.base.YcRepository
import com.yc.jetpacklib.extension.ycFlow
import com.yc.jetpacklib.net.YcResult
import kotlinx.coroutines.flow.Flow
import listToIMapper
import ycGetDataNoNull

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2021-06-30
 * UseDes:
 */
class MainTopicRepository(private val apiService: ApiService) : YcRepository() {

    private val mTopicMapper by lazy {
        TopicMapper()
    }

    private val mExamMapper by lazy {
        ExamMapper()
    }

    /**
     * 获取专题列表
     */
    fun getTopicList(projectId: String, isPass: String): Flow<YcResult<List<TopicEntity>?>> =
        ycFlow {
            val json = apiService.getTopicList(projId = projectId, isPass = isPass)
                .listToIMapper(mTopicMapper)
            send(YcResult.Success(json))
        }

    /**
     * 获取专题列表（搜索列表）
     */
    fun getTopicForNameList(
        projectId: String,
        keyName: String
    ): Flow<YcResult<List<TopicEntity>?>> = ycFlow {
        val json =
            apiService.getTopicList(projId = projectId, name = keyName).listToIMapper(mTopicMapper)
        send(YcResult.Success(json))
    }

    /**
     * 获取考试列表
     */
    fun getExamList(projectId: String, type: String): Flow<YcResult<List<ExamEntity>?>> = ycFlow {
        val json =
            apiService.getExamList(projId = projectId, groupId = type).listToIMapper(mExamMapper)
        send(YcResult.Success(json))
    }

    /**
     * 获取考试列表（搜索列表）
     */
    fun getExamForNameList(projectId: String, name: String): Flow<YcResult<List<ExamEntity>?>> =
        ycFlow {
            val json = apiService.getExamList(projId = projectId, name = name, groupId = "1")
                .listToIMapper(mExamMapper)
            send(YcResult.Success(json))
        }

    /**
     * 点击考试前获取学习时间看是否达标
     */
    fun getCheckStudyTime(projectId: String, topicId: String): Flow<YcResult<String>> = ycFlow {
        val json = apiService.getCheckStudyTime(projectId, topicId).ycGetDataNoNull()
        send(YcResult.Success(json))
    }

}